<template>
  <div class="wrap">
    <div class="login-wrap">
      <el-form :model="loginData" :rules="loginRules" label-width="80px" ref="loginForm" label-position="top" class="login-form">
        <div class="login-tip">登录</div>
        <el-form-item label="账号" prop="userName">
          <el-input clearable maxlength="10" placeholder="请输用户名" prefix-icon="el-icon-user" show-word-limit v-model="loginData.userName"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input clearable maxlength="32" placeholder="请输入密码" prefix-icon="el-icon-lock" show-password show-word-limit v-model="loginData.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button :disabled="loginBtnDisable" @click="submitForm()" class="login-btn" ref="loginBtn" type="primary">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import request from '../../util/request';
import base from '../../util/base';

export default {
  name: 'Index',
  data() {
    return {
      loginBtnDisable: false,
      loginData: {
        userName: 'admin',
        password: '123456',
      },
      loginRules: {
        userName: [
          {
            type: 'string',
            trigger: 'blur',
            validator: (rule, value, callback) => {
              const trimValue = value.trim();
              if (trimValue === '') {
                return callback(new Error('请输用户名'));
              }
              if (trimValue.length < 3 || trimValue.length > 10) {
                this.loginData.userName = trimValue;
                return callback(new Error('长度在 3 到 10 个字符'));
              }
              return callback();
            },
          },
        ],
        password: [
          {
            type: 'string',
            trigger: 'blur',
            validator: (rule, value, callback) => {
              // console.log("-" + value + "-");
              const trimValue = value.trim();
              if (trimValue === '') {
                return callback(new Error('请输入密码'));
              }
              if (trimValue.length < 6 || trimValue.length > 32) {
                this.loginData.password = trimValue;
                return callback(new Error('长度在 6 到 32 个字符'));
              }
              return callback();
            },
          },
        ],

      },
    };
  },
  methods: {
    submitForm() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          request({
            url: '/user/login',
            method: 'post',
            data: this.loginData,
          }).then((res) => {
            // console.log(res);
            const result = res.data;
            if (result.code === 0) {
              this.$router.push('/');
              this.$notify.success({
                title: '提示',
                message: '登录成功',
                duration: 1000,
              });
              base.setToken(result);
            } else {
              this.$notify.error({
                title: '提示',
                message: result.msg,
                duration: 1000,
              });
            }
          });
          return true;
        }
        return false;
      });
    },
  },
};
</script>

<style scoped>
  .wrap {
    height: 100%;
    background-color: #324152;
  }

  .login-wrap {
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .login-form {
    /*background-color: rgba(255,255,255,0.7);*/
    background-color: #fff;
    width: 400px;
    border-radius: 5px;
    padding: 20px;
  }

  .login-tip {
    font-size: 30px;
    font-weight: bold;

  }

  .login-btn {
    width: 100%;
  }
</style>
